import 'package:flutter/material.dart';
import 'package:whale/pages/im/im/widgets/card.dart';
import 'package:whale/pages/im/im/widgets/system.dart';
import 'package:whale/pages/im/im/widgets/widgets.dart';
import 'package:whale/pages/im/model/im.dart';
import 'package:whale/pages/im/util/im_define.dart';
import 'package:whale/pages/im/util/index.dart';

import '../../../../helper/utils/util.dart';
import '../../model/msg.dart';
import '../index.dart';
import 'text.dart';

/// im
class IMCell extends GetView<ImController> {
  const IMCell({
    Key? key,
    required this.md,
  }) : super(key: key);
  final ImModel md;

  Widget getContent() {
    Get.log(md.msgJson.toString());
    Get.log(md.msgType.name);
    switch (md.msgType) {
      case ImMsgType.text:
        return IMText(
          md: md,
        );
      case ImMsgType.image:
        return IMImage(
          md: md,
        );
      case ImMsgType.voice:
        return IMVoice(
          md: md,
        );
      case ImMsgType.video:
        return IMText(
          md: md,
        );
      case ImMsgType.map:
        return IMText(
          md: md,
        );
      case ImMsgType.card:
        Get.log('card');
        return IMCard(
          md: md,
        );
      case ImMsgType.file:
        return IMText(
          md: md,
        );
      case ImMsgType.system:
        return IMText(
          md: md,
        );
    }
  }

  List<Widget> getList() {
    var content = ClipRRect(
      borderRadius: BorderRadius.only(
        topLeft: md.isSelf ? const Radius.circular(5) : Radius.zero,
        topRight: !md.isSelf ? const Radius.circular(5) : Radius.zero,
        bottomLeft: const Radius.circular(5),
        bottomRight: const Radius.circular(5),
      ),
      child: Container(
        margin: const EdgeInsets.only(top: 3),
        constraints: BoxConstraints(
          minHeight: IMDefineContentConfig.minH,
          minWidth: IMDefineContentConfig.minW,
          maxWidth: IMDefineContentConfig.maxW,
        ),
        decoration: BoxDecoration(
          color:
              md.isSelf ? IMConfig.bubbleSelfColor : IMConfig.bubbleOtherColor,
        ),
        child: getContent(),
      ),
    );
    var refresh = GetBuilder<ImController>(
      id: 'refresh',
      builder: (_) {
        return InkWell(
          onTap: () => controller.resend(md),
          child: Container(
            width: 30,
            height: 30,
            alignment: Alignment.center,
            // color: Colors.amber,
            child: md.msg.status == IMMsgStatus.sending
                ? SizedBox(
                    width: 20,
                    height: 20,
                    child: CircularProgressIndicator(
                      color: IMConfig.bubbleSelfColor,
                      strokeWidth: 2,
                    ),
                  )
                : (md.msgStatus == IMMsgStatus.fail
                    ? Icon(
                        Icons.refresh,
                        color: IMConfig.bubbleSelfColor,
                      )
                    : null),
          ),
        );
      },
    );

    var list = [
      loadImage(url: md.fromUser.avater, width: 36.r, height: 36.r),
      Container(
        width: 10.r,
      ),
      SizedBox(
        width: IMDefineContentConfig.maxBoxW,
        child: Column(
          crossAxisAlignment:
              md.isSelf ? CrossAxisAlignment.end : CrossAxisAlignment.start,
          children: [
            Text(
              md.fromUser.name,
              style: TextStyle(
                color: const Color(0xff999999),
                fontSize: 10.sp,
              ),
            ),
            md.isSelf
                ? Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [refresh, content],
                  )
                : content,
          ],
        ),
      )
    ];
    if (md.isSelf) {
      return list.reversed.toList();
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 10.r, bottom: 10.r, right: 10.r),
      child: md.msgType == ImMsgType.system
          ? IMSystem(
              md: md,
            )
          : Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment:
                  md.isSelf ? MainAxisAlignment.end : MainAxisAlignment.start,
              children: getList(),
            ),
    );
  }
}
